Comparing two string arrays ???

Hi everyone, below code was supposed to be comparing two given arrays. Not to cause too much mess, for the time being i sent you only the problematic part. these two arrays are sorted alphabetically and they are string arrays. When the loop arrives at i = 20, it detects the first mismatching value between two arrays, and thus it should assign this value to w1[0], and at at i = 22 it redetects that there is a matching value. Then it should assign this value to normally to z120 since the code detects two mismatching values consecutively.

Taking into account that there is no syntax errors during the execution. And noOfAttrs1 is 55 and attrsNames1 is an array of size 55 which has 56 well-defined elements and similarly attrNames4 is an array of size 51 which has 52 well-defined elements. I am aware of the fact that either way i am going to receive an error message during the execution as the program will detect an unassigned value for attrNames452 which will be corrected afterwards. however my problem is i do not why, and i cannot see why the program can not assign a value at z122 and w120 ??? Because normally all these values are present in the array definitions and when i print a1 and x1 values until i = 52, it well prints out the array values, but it can not assign! Also instead of saying that it could not assign a value to z120 why it says that there is an unassigned element at z122 ? I do thank everyone who spares some time on this question, and i would like to underline that any help is greatly appreciated. Kind regards.
 

for(i=0; i< noOfAttrs1; i++) {
            string a1 = attrNames1[i]
            string x1 = attrNames4[i - oFFsetN]
             // print a1 "\t   i'm the first baseline's attributes\t" x1 "\t   i'm the second baseline's attributes\n"
                    if(x1 == a1) {
                         z1[sizer1++] = a1
                          print z1[i] "\n"
                        }
 
                           if(x1 != a1) {
                                w1[sizer2++] = attrNames1[i]
                                  //oFFsetP++
                                  oFFsetN++
                               // print w1[i] "\n"
                              }
}

SystemAdmin - Thu May 14 04:19:46 EDT 2009

Re: Comparing two string arrays ???
SystemAdmin - Thu May 14 04:41:16 EDT 2009

thanks everyone one more time, i solved the problem. Unfortunately i was misusing the variables in the print function from where the problem arose. Kind regards

Re: Comparing two string arrays ???
llandale - Thu May 14 10:04:59 EDT 2009

Yes, looks like you want to print z1 of sizer1, not of i. Or just print a1. I would probably print i, sizer1, and a1.

Your text is a little off. The 'sizeof' an array is the number of legal values it may hold, not sizeof+1. The range of those values is zero to sizeof-1. Your main do-loop is correct.

Your algorithm looks like it will work so long as the names in AttrNames4 is a superset of AttrNames1; that is you start with AttrNames1 and insert some values to make AttrNames4. If AttrNames1 has a value not in AttrNames4, your algorithm will stop finding any more matches. If that's not the case and both lists may have a value not in the other, then you need a two-fingered algorithm:

do while (!Done)
}  if    (i1 >= Size1) Done = true
   elseif(i4 >= Size4) Done = true
   else
   {  a1 = attrNames1[i1]
      a4 = attrNames4[i4]
      if    (a1 < a4) Deal with mismatch, i1++
      elseif(a1 > a4) Deal with mismatch, i4++
      else They are the same; i1++; i4++
   }
}

 


>Louie